package com.csu.filter;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "EncodingFilter")
public class EncodingFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest rq, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) rq;
        HttpServletResponse response = (HttpServletResponse) resp;
        String uri = request.getRequestURI();
        request.setCharacterEncoding("UTF-8");
//        response.setContentType("text/html;charset=utf-8");
        if (uri.contains("login.jsp") || uri.contains("Login") || uri.contains(".css") || uri.endsWith(".js")
                || uri.endsWith(".jpg") || uri.endsWith(".png")) {
            chain.doFilter(rq, resp);
        }
        else {
            response.setContentType("text/html;charset=utf-8");
            chain.doFilter(rq, resp);
        }
    }
}
